package com.qf.lja.util;

import java.sql.*;
import java.util.List;

public class JDBCUtil {
    private Connection conn;

    public Connection getConnection() {
        //第一步
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("驱动注册成功，可以进行数据库编程了！");
        } catch (ClassNotFoundException e) {
            System.out.println("驱动注册失败，请检查classpath或者类名拼写");
            e.printStackTrace();
            return null;
        }
        try {
//          conn = DriverManager.getConnection("jdbc:mysql://10.254.1.38:3306/homework", "developer", "Javaweb@0102");
            if (conn == null || conn.isClosed()) {
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "123456");
//                conn = DriverManager.getConnection("jdbc:mysql://10.254.1.38:3306/homework", "developer", "Javaweb@0102");
                System.out.println("数据库连接成功");
            }
            return conn;
        } catch (SQLException e) {
            System.out.println("数据库连接失败，请检查控制台异常");
            e.printStackTrace();
            return null;
        }
    }


    public boolean update(String sql) {
        getConnection();
        try {
            Statement stmt = conn.createStatement();
            //第四步：组织正确的sql语句，并通过Statement对象执行
            int rows = stmt.executeUpdate(sql);
            if (rows > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeConnection();
        }
        return false;
    }


    public ResultSet query(String sql, Object... param) {
        getConnection();//保障代码执行sql之前，已经获得连接
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            //3.1 给sql语句传参
            //可变长参数本质上是一个数组
            if (param != null) {
                for (int i = 0; i < param.length; i++) {
                    pstmt.setObject(i + 1, param[i]);
                }
            }
            ResultSet rs = pstmt.executeQuery();
            return rs;//因为ResultSet对象是依赖于连接的，
            //所以不能关闭连接。需要等待结果处理完之后才由调用者关闭

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("sql执行失败，请检查控制台异常");
        }
        return null;
    }

    public int update(String sql, Object... param) {
        getConnection();
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            if (param != null) {
                for (int i = 0; i < param.length; i++) {
                    pstmt.setObject(i + 1, param[i]);
                }
            }
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("SQL执行失败，请检查控制台异常");
        }
        return -1; // 返回一个表示失败的值
    }

    public ResultSet query(String sql, List<Object> param) {
        getConnection();
        try {
            //第三步：获得执行预编译sql容器
            PreparedStatement pstmt = conn.prepareStatement(sql);
            //3.1 给sql语句传参
            if (param != null) {
                for (int i = 0; i < param.size(); i++) {
                    pstmt.setObject(i + 1, param.get(i));

                }
            }
            //第四步：执行查询操作
            ResultSet rs = pstmt.executeQuery();
            return rs;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public void closeConnection() {
        try {
            if (conn == null || conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public boolean insert(String sql, Object... param) {
        getConnection(); // 确保在执行 SQL 之前已经获得连接
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);

            // 给 SQL 语句传递参数
            if (param != null) {
                for (int i = 0; i < param.length; i++) {
                    pstmt.setObject(i + 1, param[i]);
                }
            }

            // 执行插入操作
            int rowsAffected = pstmt.executeUpdate();

            // 检查是否有行受到影响
            if (rowsAffected > 0) {
                return true; // 插入成功
            } else {
                return false; // 插入失败
            }
        } catch (SQLException e) {
            System.out.println("SQL 执行失败，请检查控制台异常：" + e.getMessage());
            return false; // 插入失败
        } finally {
            // 在此处关闭连接
            closeConnection();
        }
    }

}
